package mylittleinvestigations;

import java.io.IOException;
import mylittleinvestigations.CaseInformation.Case;
import mylittleinvestigations.CaseInformation.CaseXmlReader;
import mylittleinvestigations.CaseInformation.CaseXmlWriter;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;

public class ForegroundElementAnimation
{
    private String animationId;
    private Animation animation;
    private Vector2 position;
    
    public ForegroundElementAnimation(String animationId, Vector2 position)
    {
        this.animationId = animationId;
        this.position = position;
    }
    
    private Animation getAnimation()
    {
        if (this.animation == null)
        {
            this.animation = Case.getInstance().getAnimationManager().GetAnimationFromId(this.animationId);
        }
        
        return this.animation;
    }
    
    public void Begin()
    {
        this.getAnimation().Begin();
    }
    
    public void Update(GameContainer gc, int delta)
    {
        this.animation.Update(gc, delta);
    }
    
    public void Draw(GameContainer gc, Graphics g, Vector2 offsetVector)
    {
        this.animation.Draw(gc, g, this.position.add(offsetVector), false /* flipHorizontally */, 1.0f);
    }
    
    public void SaveToXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("ForegroundElementAnimation");
        writer.WriteTextElement("AnimationId", this.animationId);
        writer.StartElement("Position");
        this.position.SaveToXml(writer);
        writer.EndElement();
        writer.EndElement();
    }
    
    public void markChildSprites(String markParent)
    {
        this.getAnimation().markChildSprites(markParent);
    }
    
    public ForegroundElementAnimation(CaseXmlReader reader)
    {
        reader.StartElement("ForegroundElementAnimation");
        this.animationId = reader.ReadTextElement("AnimationId");
        reader.StartElement("Position");
        this.position = new Vector2(reader);
        reader.EndElement();
        reader.EndElement();
    }
}
